<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Database;
use brand\page\Table;

class Switcher extends Component
{
    public function _init()
    {
        $this->component = 'switcher';
    }

    /**
     * switch搜索改为radio
     */
    public function buildColumns()
    {
        parent::buildColumns();

        if (!$this->is_search) return;
        $this->component = 'radio';
        $active_value = $this->getFormParam('props.active-value') ?: 1;
        $active_text = $this->getFormParam('props.active-text') ?: '开启';
        $inactive_value = $this->getFormParam('props.inactive-value') ?: 0;
        $inactive_text = $this->getFormParam('props.inactive-text') ?: '关闭';
        $data[$active_value] = $active_text;
        $data[$inactive_value] = $inactive_text;
        $this->config['form']['options'] = BrandHelper::formatOptions($data);


    }



    public function Table($this_column)
    {
        parent::Table($this_column);
        $show_type = $this->getTableParam('show_type') ?: '';
        if($show_type == 'curd' || $show_type == ''){
            $props = [
                'async' => [
                    'method' => 'post',
                    'data' => [$this->pk],
                    'url' => Table::$update_url
                ],
            ];

            $table_props = $this->getTableParam('props');
            if ($table_props){
                foreach ($table_props as $key => $table_prop) {
                    $props[$key] = $table_prop;
                }
            }
            if (!isset($props['active-value'])) $props['active-value'] = 1;
            if (!isset($props['inactive-value'])) $props['inactive-value'] = 0;
            if (is_numeric($props['active-value'])) $props['active-value'] = (int)$props['active-value'];
            if (is_numeric($props['inactive-value'])) $props['inactive-value'] = (int)$props['inactive-value'];
            $this_column->scopedSlots(
                [
                    Table::$SurfaceTable->switcher()->props($props),
                ]
            );
        }

        return $this_column;
    }

    /**
     * table表格内容
     * @param $key
     * @param $value
     */
    public function checkTableValue($key, $value)
    {

        $value = parent::checkTableValue($key, $value);
        $show_type = $this->getTableParam('show_type') ?: '';

        if($show_type == 'options'){
            if ($this->getFormParam('props.active-value') == $value){
                $value = $this->getFormParam('props.active-text');
            }elseif($this->getFormParam('props.inactive-value') == $value){
                $value = $this->getFormParam('props.inactive-text');
            }
        }

        return $value;
    }

    /**
     * 创建Curd表单
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {
        $column = \brand\Brand::Builder()->form_props($props)
            ->form_method('switcher')
            ->form_method($this->component)
            ->table_action_type($this->component)
            ->search_condition('=');
        $comment = Database::curdComment($field['comment']);

        $props['active-value'] = 1;
        $props['active-text'] = "";
        $props['inactive-value'] = 0;
        $props['inactive-text'] = "";

        if($comment['params']){
            // TODO 自定义开关内容
            $props['active-value'] = $comment['params'][0]['value'] ?? 1;
            $props['active-text'] = $comment['params'][0]['label'] ?? "开";
            $props['inactive-value'] = $comment['params'][1]['value'] ?? 0;
            $props['inactive-text'] = $comment['params'][1]['label'] ?? "关";
            if (is_numeric($props['active-value'])) $props['active-value'] = (int)$props['active-value'];
            if (is_numeric($props['inactive-value'])) $props['inactive-value'] = (int)$props['inactive-value'];

        }
        $column->form_props($props);
        $column->table_props($props);

        return $column;
    }


    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        $options['active-value'] = \brand\Brand::Builder('打开的值')->build();
        $options['active-text'] = \brand\Brand::Builder('打开时的文字描述')->build();
        $options['inactive-value'] = \brand\Brand::Builder('关闭的值')->build();
        $options['inactive-text'] = \brand\Brand::Builder('关闭时的文字描述')->build();

        $cloumn = \brand\Brand::Builder('选择器配置','arrays')
            ->form_options([$options])
            ->form_props( [
                'append'    =>  $curd_method != 'curd_method'
            ])
            ->form_visible([['prop' => $curd_method, 'value' => 'Switcher']])
            ->build();



        $props = $tableConfigValue['form']['props'] ?? [];

        $values[] = $props;

        return ['column' => $cloumn,'value' => $values];
    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'][0] ?? [];

        $props['active-value'] = $param['active-value'] ?? 1;
        $props['active-text'] = $param['active-text'] ?? '';
        $props['inactive-value'] = $param['inactive-value'] ?? 0;
        $props['inactive-text'] = $param['inactive-text'] ?? '';

        if (is_numeric($props['active-value'])) $props['active-value'] = (int)$props['active-value'];
        if (is_numeric($props['inactive-value'])) $props['inactive-value'] = (int)$props['inactive-value'];

        $form['props'] = $props;
        $table['action_type'] = 'switcher';
        $table['props'] = $props;


        return [$form,$table];
    }

}